<template>
    <div class="app-container">
        <levelbar></levelbar>
            <div class="app-box">
                <div class="appTitle">屏保设置</div>
                <div class="appTop">屏保列表</div>
                <div class="table-title">
                    <el-button  @click="addImage">添加图片</el-button>
                </div>
                <el-table :data="queryData" style="width: 100%" border  v-loading="loading" ref="multipleTable">
                    <el-table-column label="名称" align="center" min-width="120">
                         <template scope="scope">
                            {{scope.row.picName | filterPicName}}
                        </template>
                    </el-table-column>
                    <el-table-column label="屏保文字" align="center"  min-width="200">
                         <template scope="scope">
                            {{scope.row.desc||'--'}}
                        </template>
                    </el-table-column>

                    <el-table-column label="特定时间段" align="center" width="300">
                        <template scope="scope">
                            {{scope.row.rightTime||'--'}}
                        </template>
                    </el-table-column>

                    <el-table-column label="上传时间" align="center" min-width="160">
                         <template scope="scope">
                            {{scope.row.createTime.substring(0,16)||'--'}}
                        </template>
                    </el-table-column>

                    <el-table-column label="操作" align="left" width="180">
                        <template scope="scope">
                            <el-button type="text" class="btn-success" @click="check(scope.row)">查看</el-button>
                            <el-button type="text" class="btn-success" @click="edit(scope.row)">编辑</el-button>
                            <el-button type="text" class="btn-success" @click="deleteRow(scope.row.pId)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="appTop">屏保时间设置</div>
                <div class="setTime-container">
                    <el-form label-width="100px" class="app-content" :rules="rules" :model="ruleForm" ref="ruleForm">
                        <el-row>
                            <el-col :span='6' class='UpdataPb'>
                                <el-form-item label="屏保轮播：" prop='carousel'>
                                    <el-select v-model="ruleForm.carousel" style="width:100%">
                                        <el-option v-for="item in carouselList" 
                                        :key="item.pId" :label="item.label" :value="item.pId">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span='6'>
                                <el-form-item label-width='30px'>
                                    <span>屏保图片轮播一次</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span='6' class='UpdataPb'>
                                <el-form-item label="自动锁屏：" prop='lockScreen'>
                                    <el-select v-model="ruleForm.lockScreen" style="width:100%">
                                        <el-option v-for="item in lockScreenList" 
                                        :key="item.pId" :label="item.label" :value="item.pId">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span='6'>
                                <el-form-item label-width='30px'>
                                    <span>后无操作触发屏保</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </div>
                <div class="footer">
                    <el-button type="primary" @click="saveScreen('ruleForm')">保 存</el-button>
                </div>
            </div>
        <el-dialog title="查看图片" :visible.sync="dialogFormVisible" size="tiny" :before-close="handleCloseCheck">
            <div class='checkImg'><img :src="bigImgUrl"/></div>
        </el-dialog>
    </div>
</template>

<script>
import Levelbar from '../layout/Levelbar';
import TableLayoutes from 'components/TableLayoutes';
import { mapGetters, mapState, mapActions } from 'vuex';
import { updateWisdomScreensaverTime, pageWisdomScreensaver, deletepicture, getScreenSaverInfo} from 'api/wisdom';

export default {
    name: 'screensaver',
    components: { Levelbar, TableLayoutes },
    data() {
        return {
            loading: true,
            dialogFormVisible: false,
            bigImgUrl: '',
            sPosition: 1, // 再添加应该是第几条
            queryData: [],
            carouselList: [ // 轮播
                {
                    label: '5秒',
                    pId: 5
                },
                {
                    label: '8秒',
                    pId: 8
                },
                {
                    label: '10秒',
                    pId: 10
                }
            ],
            lockScreenList: [ // 锁屏
                {
                    label: '1分钟',
                    pId: 1
                },
                {
                    label: '3分钟',
                    pId: 3
                },
                {
                    label: '5分钟',
                    pId: 5
                }
            ],
            ruleForm: {
                carousel: '', // 轮播时间
                lockScreen: '' // 锁屏时间    
            },
            rules: {
                carousel: [
                    { required: true, message: '请选择屏保轮播时间', trigger: 'blur' }
                ],
                lockScreen: [
                    { required: true, message: '请选择自动锁屏时间', trigger: 'blur' }
                ]
            }
        }
    },
    created() {
        this._getScreenSaverInfo();// 屏保时间
        this._pageWisdomScreensaver();// 列表
    },
    computed: {
        ...mapGetters([
            'uid',
            'schoolId',
            'schoolName',
            'schoolType',
            'roleType'
        ]),
        ...mapState({
            config: state => state.config.table,
            cacheUpdated: state => state.app.cacheUpdated // 局部刷新列表
        })
    },
    methods: {
        ...mapActions(['ChangePageSize']),
        // 获取列表
        _pageWisdomScreensaver() {
            let json={
                fSchoolId: this.schoolId
            }
            pageWisdomScreensaver(json).then(res => {
                this.loading=false;
                if (res.data.content[0]===null&&res.data.content.length==1) {
                    this.queryData=[]
                    res.data.content=[];
                } else {
                    this.queryData = res.data.content;
                }
                if (res.data.content&&res.data.content.length) {
                    this.sPosition=res.data.content.length+1;
                }
            }).catch(() => {
                this.loading=false;
            })
        },
        // 添加图片按钮
        addImage() {
            if (this.sPosition>=9) {
                this.$message.info('最多添加8张屏保图片')
            } else {
                this.$router.push({ path: `/screensaver/addScreenImg/${this.sPosition}`});
            }
        },
        // 点击查看
        check(row) {
            this.dialogFormVisible = true;
            this.checkStatus = true;
            this.bigImgUrl = row.picture;
        },
        handleCloseCheck() {
            this.dialogFormVisible = false;
        },
        // 点击编辑
        edit(row) {
            this.$router.push({ path: `/screensaver/screenImgEdit/${row.pId}/${this.sPosition}`});
        },
        // 删除
        deleteRow(pId) {
            this.$confirm('是否确认删除该屏保图片？', ' ', {
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(() => {
                this.loading=true;
                let json={pId, fSchoolId: this.schoolId}
                deletepicture(json).then(response => {
                    this._pageWisdomScreensaver();// 刷新
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    })                   
                });  
            }).catch((e) => {       
                this.loading=false
            });
        },
          // 保存屏保
        saveScreen(formName) {
            this.$confirm('是否确认修改?', ' ', {
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(() => {
                this.loading = true;
                this._updateWisdomScreensaverTime();
            })
        },
        _updateWisdomScreensaverTime() {
            let json={
                fSchoolId: this.schoolId,
                sLockMinute: this.ruleForm.lockScreen, // 锁屏时间
                sMinute: this.ruleForm.carousel // 轮播时间
            };
            updateWisdomScreensaverTime(json).then(res => {
                this.loading = false;
                this.$message.success('屏保设置成功')
                this._getScreenSaverInfo();
            }).catch(() => {
                this.loading = false;
            })
        },
        // 刷新屏保时间设置
        _getScreenSaverInfo() {
            let json={
                fSchoolId: this.schoolId
            }
            getScreenSaverInfo(json).then(res => {
                if (res.data.content) {
                    this.ruleForm.carousel = res.data.content.sMinute || 5;
                    this.ruleForm.lockScreen = res.data.content.sLockMinute || 1;
                } else {
                    this.ruleForm.carousel = 5;
                    this.ruleForm.lockScreen = 1;
                }          
            }).catch(() => {
                this.loading = false;
            })
        }
    }
}
</script>

<style lang="scss" scoped>
.app-container {
    position: relative;
    padding: 20px 20px 10px;
    .app-box{
        box-shadow:  0px 0px 5px 0px rgba(0, 0, 0, 0.1);
        -webkit-box-shadow:  0px 0px 5px 0px rgba(0, 0, 0, 0.1);
        background:#fff;
        padding: 0px 50px;
        padding-bottom:50px;
        .appTitle{
            padding-top:8px;
            text-align: center;
            font-size: 18px;
            color: #333;
            line-height: 72px;
            font-weight: bold;
            font-family: Microsoft YaHei;
            border-bottom: 1px solid #ddd;
        }
        .table-title{
            margin:20px 0;
        }
        .appTop{
            position: relative;
            font-size: 14px;
            color: rgb(106, 85, 72);
            padding-left: 16px;
            margin: 30px 0;
            display: block;
            &::after {
                position: absolute;
                content:'';
                width: 4px;
                height: 14px;
                background: #ff5e2c;
                left: 0;
                top: 3px;
            }
        }
        .footer{
            text-align:center;
            margin-top:90px;
        }
    }
}
</style>